package com.xyzboom.utils;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

public class WebUtils {
    public static void forward(String pageUrl, HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        if (request == null) {
            return;
        }
        request.getRequestDispatcher(pageUrl).forward(request, response);
    }

    public static String resolveUrl(String url, HttpServletRequest request) {
        return url.startsWith("/") ? request.getContextPath() + url : url;
    }

    public static void redirect(String url, HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.sendRedirect(resolveUrl(url, request));
    }
}
